package com.zr.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zr.dao.UserDao;
import com.zr.model.User;
import com.zr.util.JDBCUtil;

public class UserDaoImpl implements UserDao{

	@Override
	public User getUserByUnameAndUpsw(String uname, String upsw) {
		User user = new User();
		Connection conn = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT uid,uname,upsw,rid FROM e_user WHERE uname=? AND upsw=MD5(?) ");
		try {
			PreparedStatement pst = conn.prepareStatement(sql.toString());
			pst.setString(1, uname);
			pst.setString(2, upsw);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpsw(rs.getString("upsw"));
				user.setRid(rs.getInt("rid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

}
